#!/usr/bin/env python
# scripts/load_questions.py

import os
import json
import django

# 1. 设置环境变量，改成你自己项目的 settings 模块路径
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'InterviewAgent.settings')
django.setup()

from Interview.models import Question  # 改为你自己 app 的 import 路径


def run():
    # 2. 读取我们之前准备的 questions_fixture.json（或者 questions.json）
    with open('D:/实验室/InterviewAgent/questions_fixture.json', encoding='utf-8') as f:
        data = json.load(f)

    questions = []
    for idx, item in enumerate(data, start=1):
        try:
            q = Question(
                content=item['content'],
                role=item['role'],
                difficulty=item['difficulty'],
                question_type=item['question_type'],
                suggested_answer_time=item['suggested_answer_time'],
                key_points=item['key_points'],
                reference_answer=item['reference_answer'],
            )
            questions.append(q)
        except KeyError as e:
            print(f"Skipped item {idx}: missing field {e}")

    # 3. 批量插入
    Question.objects.bulk_create(questions)
    print(f'✅ 成功插入 {len(questions)} 条题目')


if __name__ == '__main__':
    run()
